host = 'http://192.168.116.128:38000/'

import requests


def response_info(func):  # 查看响应体信息
    print('响应头为：', func.headers)
    print('响应文本为：', func.json())  # 使用json方法，避免转义字符
    print('响应码为：', func.status_code)
    print('响应时间为：', func.elapsed.total_seconds())


# 1.1登录鉴权接口
def seafile_login(username, password):
    url = host + 'api2/auth-token/'
    header = {'content-type': 'application/x-www-form-urlencoded'}
    body = {'username': username, 'password': password}
    resp = requests.post(url, headers=header, data=body)
    # print('---' * 15, 'seafile_login', '---' * 15)
    return resp


# 1.2 获取账号信息
def seafile_account_info():
    url = host + 'api2/account/info/'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    resp = requests.get(url, headers=header)
    # print('---' * 15, 'seafile_account_info', '---' * 15)
    return resp


# 1.3新增资料库接口
def seafile_repo_add(name):
    url = host + 'api2/repos/'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    bodys = {'name': name}
    resp = requests.post(url, headers=header, data=bodys)
    # print('---' * 15, 'seafile_account_info', '---' * 15)
    return resp


# 1.4查询所有的资料库接口
def seafile_query_repo():
    url = host + 'api2/repos/'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    resp = requests.get(url, headers=header)
    # print('---' * 15, 'seafile_account_info', '---' * 15)
    return resp


# 1.5 修改资料库名字
def seafile_modify_repo(old_name, new_name):
    target_id = None
    for repo in seafile_query_repo().json():
        if repo['name'] == old_name:
            target_id = repo['id']
    url = host + f'api2/repos/{target_id}/?op=rename'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    bodys = {'repo_name': new_name}
    resp = requests.post(url, headers=header, data=bodys)
    # print('---' * 15, 'seafile_account_info', '---' * 15)
    return resp


# 1.6删除指定的资料库接口
def seafile_delet_repo(name):
    target_id = None
    for repo in seafile_query_repo().json():
        if repo['name'] == name:
            target_id = repo['id']
    url = host + f'api2/repos/{target_id}/'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    resp = requests.delete(url, headers=header)
    return resp


# 1.7在指定的资料库目录新建文件接口
def seafile_add_file(repo_name, file_name):
    target_id = None
    for repo_id in seafile_query_repo().json():
        if repo_id['name'] == repo_name:
            target_id = repo_id['id']
    url = host + f'api2/repos/{target_id}/file/?p=/{file_name}&reloaddir=true'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    bodys = {'operation': 'create'}
    resp = requests.post(url, headers=header, data=bodys)
    return resp


# 1.8在指定的资料库目录删除文件接口
def seafile_delete_file(repo_name, file_name):
    target_id = None
    for repo in seafile_query_repo().json():
        if repo['name'] == repo_name:
            target_id = repo['id']
    url = host + f'api2/repos/{target_id}/file/?p=/{file_name}&reloaddir=true'
    token = seafile_login('seafile@admin.com', 'admin').json().get('token')
    header = {'Authorization': 'Token ' + token}
    resp = requests.delete(url, headers=header)
    return resp

# 测试：调用响应体接口查询函数
# response_info(seafile_modify_repo('坤坤写真群', '坤坤判刑群'))
